package com.mes.controller;

import com.mes.entity.User;
import com.mes.service.QualityCheckService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("qualityCheck")
public class QualityCheckController {
	@Autowired
	private QualityCheckService qualityCheckService;
	
	//初始化页面的基本信息
	@ResponseBody
	@RequestMapping(value="initBaseInfo",method=RequestMethod.POST)
	public Message initBaseInfo(HttpServletRequest request){
		Message msg = new Message();
		List<Map<String,Object>> results = qualityCheckService.initResultInfo();
		if(null != results){
			Map<String,Object> tmpMap = new HashMap<String,Object>();
			tmpMap.put("NAME","请选择");
			tmpMap.put("VALUE",null);
			results.add(tmpMap);
		}
		
		List<Map<String,Object>> reasons = qualityCheckService.initPoorReasonInfo();
		if(null != reasons){
			Map<String,Object> tmpMap = new HashMap<String,Object>();
			tmpMap.put("NAME","请选择");
			tmpMap.put("CODE",null);
			reasons.add(tmpMap);
		}
		
		List<Map<String,Object>> processInfo = qualityCheckService.initProcessInfo();
		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		Map<String,Object> defaultProcess = qualityCheckService.getDefaultProcessInfo(user.getId());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("results", results);
		map.put("reasons", reasons);
		map.put("processInfo", processInfo);
		map.put("defaultProcess", defaultProcess);
		msg.setData(map);
		return msg;
	}
	
	//保存质检判定结果信息
	@ResponseBody
	@RequestMapping(value="saveQualityCheckResult",method=RequestMethod.POST)
	public Message saveQualityCheckResult(String emesoManuBatchId,String result,String r_emesProcessId,String reason,String description,String qty,HttpServletRequest request){
		Message msg = new Message();
		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		if(qualityCheckService.saveQualityCheckResult(emesoManuBatchId,result,r_emesProcessId,reason,description,user,qty)){
			msg.setOkMessage("保存成功！");
		}else{
			msg.setErrorMessage("保存失败！");
		}
		return msg;
	}
}
